package com.markpim.concurrency.opdr3;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.markpim.concurrency.opdr3.RobbenEiland.Citizen;
import com.markpim.concurrency.opdr3.RobbenEiland.Guard;
import com.markpim.concurrency.opdr3.RobbenEiland.VIP;

public class UnieGebouw {

	private Lock lock;
	private Condition noCitizensInside;
	private Condition vipInside;
	private Condition citizensWaiting;
	private Condition vipsWaiting;

	private int guardCount;

	public UnieGebouw() {
		lock = new ReentrantLock();

		noCitizensInside = lock.newCondition();
		vipInside = lock.newCondition();
		citizensWaiting = lock.newCondition();
		vipsWaiting = lock.newCondition();
	}

	void signIn() {

		lock.lock();

		try {

			Thread t = Thread.currentThread();

			if (t instanceof Citizen) {

				System.out.println("Citizen signs in");

			} else if (t instanceof VIP) {
				System.out.println("VIP signs in");

			} else if (t instanceof Guard) {
				System.out.println("Guard signs in");
				guardCount++;
			} else
				System.err.println("error");

		} finally {
			lock.unlock();
		}
	}

	void signOut() {

		lock.lock();

		try {

			Thread t = Thread.currentThread();

			if (t instanceof Citizen) {
				System.out.println("Citizen signs out");

			} else if (t instanceof VIP) {
				System.out.println("VIP signs out");

			} else if (t instanceof Guard) {
				System.out.println("Guard signs out");
				guardCount--;
			} else
				System.err.println("error");

		} finally {
			lock.unlock();
		}

	}

}
